package com.kennedy.atoupdate.service;

import com.github.pagehelper.PageHelper;
import com.kennedy.atoupdate.beans.DeviceInfo;
import com.kennedy.atoupdate.beans.dto.UpdateGroupRequest;
import com.kennedy.atoupdate.mapper.DeviceInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by tao.zeng on 2017/8/15.
 */
@Service
public class DeviceInfoService {
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    public DeviceInfo findByDeviceId(String deviceId) {
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDeviceId(deviceId);
        return deviceInfoMapper.selectOne(deviceInfo);
    }

    public void insert(DeviceInfo deviceInfo) {
        deviceInfoMapper.insert(deviceInfo);
    }

    public List<DeviceInfo> getListByPage(DeviceInfo deviceInfo) {
        if (deviceInfo.getOffset() != null && deviceInfo.getLimit() != null) {
            PageHelper.offsetPage(deviceInfo.getOffset(), deviceInfo.getLimit());
        }
        List<DeviceInfo> deviceInfos = deviceInfoMapper.getListByPage(deviceInfo);
        return deviceInfos;
    }

    public void delete(Long id) {
        deviceInfoMapper.deleteByPrimaryKey(id);
    }

    public void updateGroup(UpdateGroupRequest updateGroupRequest) {
        for (Long id : updateGroupRequest.getIds()) {
            DeviceInfo deviceInfo = new DeviceInfo();
            deviceInfo.setId(id);
            deviceInfo.setGroupId(updateGroupRequest.getGroupId());
            deviceInfoMapper.updateByPrimaryKeySelective(deviceInfo);
        }

    }
}
